package com.finance.system.controller;

import com.finance.base.constant.BaseConstants;
import com.finance.base.exception.BaseException;
import com.finance.framework.controller.BaseController;
import com.finance.framework.controller.Result;
import com.finance.framework.lang.BeanUtils;
import com.finance.framework.qurtz.TaskDescriptor;
import com.finance.scheduler.requestxml.PullDataBackRequest;
import com.finance.scheduler.requestxml.PullDataRequest;
import com.finance.scheduler.responsexml.PullLogistics;
import com.finance.scheduler.service.*;
import com.finance.system.bean.JobReq;
import com.finance.system.bean.JobStateDesciptor;
import com.finance.system.bean.TaskReq;
import com.finance.system.bean.TriggerReq;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 任务调度管理
 * Created by CBWL on 2017/3/9.
 */
@Controller
@RequestMapping("/job")
public class JobController extends BaseController{
    @Resource
    private QuartzManager quartzManager;

    @Autowired
    private TaskFinder taskFinder;

    @RequestMapping("/listAll")
    public Result listAll() throws BaseException{
        List<TaskDescriptor> jobs =  this.taskFinder.getAppTaskDesciptors();
        return this.getSuccessResult(jobs);
    }
    @RequestMapping("/listAvilable")
    public Result listAvilable() throws BaseException{
        try {
            List<TaskDescriptor> jobs = this.quartzManager.getAllJobs();
            return this.getSuccessResult(jobs);
        } catch (SchedulerException e) {
            throw new BaseException(BaseConstants.ERR_UNKOWN);
        }
    }
    @RequestMapping("/listJobState")
    public Result listJobState() throws BaseException{
        try {
            List<JobStateDesciptor> jobs = this.quartzManager.getAllJobStateDesciptor();
            return this.getSuccessResult(jobs);
        } catch (Exception e) {
            throw new BaseException(BaseConstants.ERR_UNKOWN);
        }
    }
    @RequestMapping("/addJob")
    public Result addJob(@Valid TaskReq taskReq) throws BaseException{
        TaskDescriptor task = new TaskDescriptor();
        try {
            BeanUtils.copyProperties(task,taskReq);
        } catch (Exception e) {
            throw new BaseException(BaseConstants.ERR_UNKOWN,e.getMessage());
        }
        this.quartzManager.addJob(task);
        return this.getSuccessResult();
    }
    @RequestMapping("/addAndScheduleJob")
    public Result addAndScheduleJob(@Valid TaskReq taskReq) throws BaseException{
        TaskDescriptor task = new TaskDescriptor();
        try {
            BeanUtils.copyProperties(task,taskReq);
        } catch (Exception e) {
            throw new BaseException(BaseConstants.ERR_UNKOWN,e.getMessage());
        }
        this.quartzManager.addAndScheduleJob(task);
        return this.getSuccessResult();
    }
    @RequestMapping("/scheduleJob")
    public Result scheduleJob(@Valid TriggerReq triggerReq) throws BaseException{
        TaskDescriptor task = new TaskDescriptor();
        try {
            BeanUtils.copyProperties(task,triggerReq);
        } catch (Exception e) {
            throw new BaseException(BaseConstants.ERR_UNKOWN,e.getMessage());
        }
        this.quartzManager.startJob(task);
        return this.getSuccessResult();
    }
    @RequestMapping("/pauseJob")
    public Result pauseJob(@Valid TriggerReq triggerReq) throws BaseException{
        TaskDescriptor task = new TaskDescriptor();
        try {
            BeanUtils.copyProperties(task,triggerReq);
        } catch (Exception e) {
            throw new BaseException(BaseConstants.ERR_UNKOWN,e.getMessage());
        }
        this.quartzManager.pauseJob(task);
        return this.getSuccessResult();
    }
    @RequestMapping("/removeJob")
    public Result removeJob(@Valid JobReq jobReq) throws BaseException{
        TaskDescriptor task = new TaskDescriptor();
        try {
            BeanUtils.copyProperties(task,jobReq);
        } catch (Exception e) {
            throw new BaseException(BaseConstants.ERR_UNKOWN,e.getMessage());
        }
        this.quartzManager.removeJob(task);
        return this.getSuccessResult();
    }

}
